<?php
namespace app\admin\model;

use think\Db;
use think\facade\Config;
use think\Model;

class TestPhone extends Model
{
    protected $table = 'bus_test_phone';
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'create_time';

    public static function add($request)
    {
        $phoneInfo = self::where('phone',$request['phone'])->find();
        if ($phoneInfo) {
            return ['code' => 422, 'msg' => '号码已存在，请勿重复添加'];
        }else{
            $phonenumber = $request['phone'];
            $phone_prefix = substr($phonenumber,0,7);
            $phoneLocation = db('info_phone_location')->cache('ipl'.$phonenumber,43200)->where('phone_prefix',$phone_prefix)->find();
            if ($phoneLocation) {
                switch ($phoneLocation['isp']) {
                    case '中国移动':
                        $request['isp'] = '10086';
                        break;
                    case '中国联通':
                        $request['isp'] = '10010';
                        break;
                    case '中国电信':
                        $request['isp'] = '10000';
                        break;
                    default:
                        break;
                }
            }
            self::create($request);
            return ['code' => 200, 'msg' => '添加成功'];
        }
    }

    public static function batchadd($request)
    {
        foreach ($request as $val) {
            $phoneInfo = self::where('phone',$val)->find();
            $row = [
                'phone' =>$val,
            ];
            if (!$phoneInfo) {
                $phone_prefix = substr($val,0,7);
                $phoneLocation = db('info_phone_location')->cache('ipl'.$val,43200)->where('phone_prefix',$phone_prefix)->find();
                if ($phoneLocation) {
                    switch ($phoneLocation['isp']) {
                        case '中国移动':
                            $row['isp'] = '10086';
                            break;
                        case '中国联通':
                            $row['isp'] = '10010';
                            break;
                        case '中国电信':
                            $row['isp'] = '10000';
                            break;
                        default:
                            break;
                    }
                }
                self::create($row);
            }
        }
        return true;
    }

    public static function upd($request)
    {
        $phoneInfo = self::where('id',$request['id'])->find();
        if ($phoneInfo['phone'] != $request['phone']) {
            $newPhone = self::where('phone',$request['phone'])->find();
            if ($newPhone) {
                return ['code' => 422, 'msg' => '号码已存在，请勿重复添加'];
            } else {
                $phonenumber = $request['phone'];
                $phone_prefix = substr($phonenumber,0,7);
                $phoneLocation = db('info_phone_location')->cache('ipl'.$phonenumber,43200)->where('phone_prefix',$phone_prefix)->find();
                if ($phoneLocation) {
                    switch ($phoneLocation['isp']) {
                        case '中国移动':
                            $request['isp'] = '10086';
                            break;
                        case '中国联通':
                            $request['isp'] = '10010';
                            break;
                        case '中国电信':
                            $request['isp'] = '10000';
                            break;
                        default:
                            break;
                    }
                }
                self::update($request, ['id' => $request['id']]);
                return ['code' => 200, 'msg' => '更新成功'];
            }
        }else{
            self::update($request);
            return ['code' => 200, 'msg' => '更新成功'];
        }
    }

    public static function getTestPhone($request)
    {
        $where = [];

        if($request['phone'] != ''){
            $where[] = ['phone', '=', $request['phone']];
        }
        if($request['isp_host'] != ''){
            $where[] = ['isp', '=', $request['isp_host']];
        }
        return self::where($where)
                ->paginate($request['limit'], false, $request)
                ->each(function($item, $key){
                    switch ($item->status) {
                        case '0':
                            $item->status = '<span class="layui-badge status-jy">禁用</span>';
                            break;
                        case '1':
                            $item->status = '<span class="layui-badge layui-bg-green status-qy">正常</span>';
                            break;
                        default:
                            $item->status = '未知';
                            break;
                    }
                    switch ($item->isp) {
                        case '10086':
                            $item->isp = '移动';
                            break;
                        case '10000':
                            $item->isp = '电信';
                            break;
                        case '10010':
                            $item->isp = '联通';
                            break;
                        default:
                            $item->isp = '未知';
                            break;
                    }
                })
                ->toArray();
    }

}
